package com.itheima.behavior.server.impl;

import com.alibaba.fastjson.JSON;
import com.heima.model.common.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.behavior.pojos.ApCollection;
import com.heima.model.common.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.CollectionBehaviorDTO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.mess.app.NewBehaviorDTO;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.common.user.pojo.ApUser;
import com.itheima.behavior.server.ApBehaviorEntryService;
import com.itheima.behavior.server.ApCollectionBehaviorService;
import com.itheima.common.constants.article.HotArticleConstants;
import com.itheima.common.exception.CustException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
@Slf4j
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = user.getId();
        //根据当前登录用户id查询行为实体对象查询 若不存在 则增加一个

        ApBehaviorEntry entry = mongoTemplate.findOne(Query.query(Criteria.where("refId").is(userId)), ApBehaviorEntry.class);
        if (entry==null){
            entry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        }
        //如果是收藏操作 判断是否收藏
        if (dto.getType()==0){//收藏
            ApCollection one = mongoTemplate.findOne(Query.query(Criteria.where("entryId").is(entry.getId()).and("articleId").is(dto.getArticleId())), ApCollection.class);
            if (one==null){
                ApCollection apCollection = new ApCollection();
                apCollection.setCollectionTime(new Date());
                apCollection.setArticleId(dto.getArticleId());
                apCollection.setType(dto.getType());
                apCollection.setEntryId(entry.getId());


                ApCollection save = mongoTemplate.save(apCollection);
                return ResponseResult.okResult();
            }
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"不能重复点收藏");
        }
        //取消不喜欢
        mongoTemplate.remove(Query.query(Criteria.where("entryId").is(entry.getId()).and("articleId").is(dto.getArticleId())),ApCollection.class);

        NewBehaviorDTO mess = new NewBehaviorDTO();
        mess.setType(NewBehaviorDTO.BehaviorType.COLLECTION);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(dto.getOperation()==0?1:-1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));
        log.info("点赞行为 发送消息 ==>{}",JSON.toJSONString(mess));
        return ResponseResult.okResult();





    }
}
